package org.hit.burkun.cases.pipline;

import org.hit.burkun.db.DBHelper;
import org.hit.burkun.db.DBTypeConverter;
import org.hit.burkun.dbimport.FileBaseImport;
import org.hit.burkun.file.FileProcessHelper.FILE_TYPE;
import org.hit.burkun.util.GlobalData;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OMIMMorbidmapImport extends FileBaseImport {

	public OMIMMorbidmapImport(String fullFileName, FILE_TYPE fileType,
			String tableName, DBHelper dbHelper, int totalPropertyNum,
			boolean hasAutoIncreasementID, DBTypeConverter typeConverter,
			LINE_SPLITER spliterType) {
		super(fullFileName, fileType, tableName, dbHelper, totalPropertyNum,
				hasAutoIncreasementID, typeConverter, spliterType);
	}

	public static void importMorbidMap() {
		String fileName = GlobalData.getInstance().getOmimMorbidmapPath();
		final DBHelper dbHelper = DBHelper.getLocalBioSearch();
		int totalPropertyNum = 5;
		FILE_TYPE fileType = FILE_TYPE.PLAIN;
		String tableName = "omim_morbidmap";
		int beginLine = 1;
		boolean hasAutoIncreasementID = false;
		LINE_SPLITER spliterType = LINE_SPLITER.SHUXIAN;

		DBTypeConverter.DATATYPE[] types = new DBTypeConverter.DATATYPE[totalPropertyNum];
		types[1] = DBTypeConverter.DATATYPE.I;
		types[3] = DBTypeConverter.DATATYPE.I;
		DBTypeConverter typeConverter = new DBTypeConverter(types);
		FileBaseImport imp = new OMIMMorbidmapImport(fileName, fileType,
				tableName, dbHelper, totalPropertyNum, hasAutoIncreasementID,
				typeConverter, spliterType);

		imp.doImport(beginLine);
	}
	
	public static void importMorbidMapNew() {
		String fileName = GlobalData.getInstance().getOmimMorbidmapPath();
		final DBHelper dbHelper = DBHelper.getLocalBioSearch();
		int totalPropertyNum = 5;
		FILE_TYPE fileType = FILE_TYPE.PLAIN;
		String tableName = "omim_morbidmap_new";
		int beginLine = 1;
		boolean hasAutoIncreasementID = false;
		LINE_SPLITER spliterType = LINE_SPLITER.SHUXIAN;

		DBTypeConverter.DATATYPE[] types = new DBTypeConverter.DATATYPE[totalPropertyNum];
		types[1] = DBTypeConverter.DATATYPE.I;
		types[3] = DBTypeConverter.DATATYPE.I;
		DBTypeConverter typeConverter = new DBTypeConverter(types);
		FileBaseImport imp = new OMIMMorbidmapImport(fileName, fileType,
				tableName, dbHelper, totalPropertyNum, hasAutoIncreasementID,
				typeConverter, spliterType);

		imp.doImport(beginLine);
	}


	private Pattern p = Pattern.compile(".*([0-9]{6}).*");

	@Override
	public String[] forEachItem(String[] item) {
		String[] newItem = new String[item.length + 1];
		newItem[0] = item[0];
		for (int i = 2; i < newItem.length; i++) {
			newItem[i] = item[i - 1];
		}
		if (item[0] != null) {
			Matcher m = p.matcher(item[0]);
			// 只提取一个
			if (m.find()) {
				newItem[1] = m.group(1);
			}
		}
		return newItem;
	}
}
